package nz.ac.massey.xmldad.client;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Hashtable;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;

import org.nutj.swing.PrettyDialog;

/**
 * User interface for constructing a message to send as 
 * a request to a web server.
 * @author Nathan C Jones
 * @version 0.1
 */
public class SwingGUI extends JFrame{
	
	private static final long serialVersionUID = -1896721834721724963L;

	public static void main(String[] args){
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Exception e) {
			e.printStackTrace();
		}
		SwingGUI frame = new SwingGUI();
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		frame.setLocation((screenSize.width-frame.getWidth())/2,(screenSize.height-frame.getHeight())/2);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
	}

	private static final String DEFAULT_URL = "http://localhost:8080/Catalogue/catalogue";
	
	private static final String BOOK_SUBMISSION = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n" +
			"<bookSubmission xmlns=\"http://xmldad.massey.ac.nz/bookQuery\" xmlns:book=\"http://xmldad.massey.ac.nz/bookCatalogue\">\n" +
			"\t<book>\n" +
			"\t\t<book:title>title</book:title>\n" +
			"\t\t<book:author>author</book:author>\n" +
			"\t\t<book:year>1999</book:year>\n" +
			"\t\t<book:isbn>0-000-00000-0</book:isbn>\n" +
			"\t\t<book:rating>0.0</book:rating>\n" +
			"\t</book>\n" +
			"</bookSubmission>\n";
	
	private static final String BOOK_SEARCH = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n" +
			"<bookSearch xmlns=\"http://xmldad.massey.ac.nz/bookQuery\">\n" +
			"\t<title>title</title>\n" +
			"\t<author>author</author>\n" +
			"\t<year>year</year>\n" +
			"\t<isbn>0-000-00000-0</isbn>\n" +
			"</bookSearch>\n";
	
	private static final String FEEDBACK_SUBMISSION = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n" +
			"<feedbackSubmission xmlns=\"http://xmldad.massey.ac.nz/bookQuery\" xmlns:feedback=\"http://xmldad.massey.ac.nz/bookCatalogue\">\n" +
			"\t<isbn>0-000-00000-0</isbn>\n" +
			"\t<feedback>\n" +
			"\t\t<feedback:comment>comment</feedback:comment>\n" +
			"\t\t<feedback:rating>10.0</feedback:rating>\n" +
			"\t\t<feedback:author>author</feedback:author>\n" +
			"\t</feedback>\n" +
			"</feedbackSubmission>";
		
	private static final Hashtable<String,String> templates = new Hashtable<String,String>();
	static{
		templates.put("None","");
		templates.put("Book Submission",BOOK_SUBMISSION);
		templates.put("Book Search",BOOK_SEARCH);
		templates.put("Feedback Submission",FEEDBACK_SUBMISSION);
	}
	
	private JTextArea xmlTextArea;
	private JTextField urlTextField;
	
	public SwingGUI(){
		super("XmlDad Client");
		init();
	}

	/**
	 * 
	 */
	private void init(){
		Container contentPane = getContentPane();
		contentPane.setLayout(new BorderLayout());
		JPanel headerPanel = new JPanel(new BorderLayout());
		JLabel headerLabel = new JLabel("Use xml to describe an object to send to the server");
		Font font = headerLabel.getFont();
		headerLabel.setFont(font.deriveFont(font.getSize()+2f));
		headerPanel.add(headerLabel,BorderLayout.NORTH);
		
		JPanel templateSelectionPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		templateSelectionPanel.add(new JLabel("Template:"));
		final JComboBox templateSelection = new JComboBox(templates.keySet().toArray());
		templateSelection.setSelectedItem("None");
		templateSelection.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				xmlTextArea.setText(templates.get(templateSelection.getSelectedItem()));
			}
		});
		templateSelectionPanel.add(templateSelection);
		headerPanel.add(templateSelectionPanel,BorderLayout.SOUTH);
		
		contentPane.add(headerPanel,BorderLayout.NORTH);
		
		xmlTextArea = new JTextArea();
		JScrollPane scrollPane = new JScrollPane(xmlTextArea,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		scrollPane.setPreferredSize(new Dimension(400,300));
		contentPane.add(scrollPane,BorderLayout.CENTER);
		
		JPanel urlPanel = new JPanel(new BorderLayout());
		urlPanel.add(new JLabel("URL:"),BorderLayout.WEST);
		urlTextField = new JTextField(DEFAULT_URL);
		urlPanel.add(urlTextField,BorderLayout.CENTER);
		
		JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		JButton sendButton = new JButton("Send");
		sendButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				sendXml();
			}
		});
		buttonPanel.add(sendButton);
		JPanel footerPanel = new JPanel(new BorderLayout());
		footerPanel.add(urlPanel,BorderLayout.NORTH);
		footerPanel.add(buttonPanel,BorderLayout.EAST);
		
		contentPane.add(footerPanel,BorderLayout.SOUTH);
		pack();
	}

	private void sendXml(){
		try {
			final String responseText = new XmlDadClient().sendMessage(xmlTextArea.getText(),urlTextField.getText()).replaceAll("><",">\n<");;
			new PrettyDialog(this, "Response"){
				
				@Override
				protected JPanel getContentPanel(){
					JTextArea textArea = new JTextArea(responseText);
					textArea.setEditable(false);
					JScrollPane scrollPane = new JScrollPane(textArea,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
					scrollPane.setPreferredSize(new Dimension(400,300));
					JPanel panel = new JPanel(new BorderLayout());
					panel.add(scrollPane);
					return panel;
				}

				@Override
				protected Object getResult(){
					return null;
				}
			}.open();
		} catch (Exception e) {
			JOptionPane.showMessageDialog(this,e.getMessage(),"error",JOptionPane.ERROR_MESSAGE);
			e.printStackTrace();
		}
	}
}
